package com.atguigu.tingshu.account.mq;

import cn.hutool.core.util.StrUtil;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
@Slf4j
public class AccountReceiver {


    private final UserAccountService userAccountService ;
    private final RechargeInfoService rechargeInfoService;
    @Autowired
    public AccountReceiver(UserAccountService userAccountService, RechargeInfoService rechargeInfoService) {
        this.userAccountService = userAccountService;
        this.rechargeInfoService = rechargeInfoService;
    }
    /**
     * @description:  小程序授权登录_初始化用户的账户数据
     * @author Petrichor
     * @date 2024/1/9 18:59
     * @return
    **/
    @KafkaListener(topics = KafkaConstant.QUEUE_USER_REGISTER)
    public void addUserAccount(ConsumerRecord<String , String> consumerRecord) {
        // 获取到新注册的用户的id
        String userId = consumerRecord.value();
        if(StringUtils.isEmpty(userId)) {
            return ;
        }
        userAccountService.addAccount(userId) ;
    }
    /**
     * @description: com.atguigu.tingshu.order.strategy.impl.AccountSaveOrderStrategy  发送扣减余额消息
     * @author Petrichor
     * @date 2024/1/9 19:04
     * @param record .value()  orderNo 订单号
     * @return NULL
    **/
    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_MINUS)
    public void minus(ConsumerRecord<String, String> record) {
        String orderNo = record.value();
        if (StringUtils.isEmpty(orderNo)) {
            return;
        }
        // 扣减锁定金额
        userAccountService.minus(orderNo);
    }
    /**
     * @description: com.atguigu.tingshu.order.strategy.impl.AccountSaveOrderStrategy 发送 解除锁定金额消息
     * @author Petrichor
     * @date 2024/1/9 20:04
     * @param record .value()  orderNo 订单号
     * @return NULL
    **/
    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_UNLOCK)
    public void unlock(ConsumerRecord<String, String> record) {
        String orderNo = record.value();
        if (StringUtils.isEmpty(orderNo)) {
            return;
        }
        // 解除锁定金额
        userAccountService.unlock(orderNo);
    }
    /**
     * @description: 更新充值的消息
     * @author Petrichor
     * @date 2024/1/10 20:21
     * @param record .value() 订单号
     * @return
     **/
    @KafkaListener(topics = KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS)
    public void rechargePaySuccess(ConsumerRecord<String, String> record) {
        String orderNo = record.value();
        if(!StrUtil.isEmpty(orderNo)) {
            log.info("充值通知: {}", orderNo);
            rechargeInfoService.rechargePaySuccess(orderNo);
        }
    }
}
